const { tenantsModel } = require('./models/tenantsModel')



module.exports.information = async function () {

};


module.exports.auditTenants = async function (msg) {
    const { searchType, searchValue, currentPage, pageSize } = msg
    //获取数据总条数
    const total = await tenantsModel.countDocuments();
    //计算总页数
    const pages = Math.ceil(total / msg.pageSize);
    //查询页数
    let chaxunye = 0;
    let tableData = []
    if (!searchValue) {
        chaxunye = await tenantsModel.find().countDocuments()
        tableData = await tenantsModel.find()
            //页数
            .limit(msg.pageSize - 0).skip((msg.currentPage - 1) * msg.pageSize);
    } else {
        chaxunye = await tenantsModel.find({
            [searchType]: searchValue - 0
        }).countDocuments();

        tableData = await tenantsModel.find({
            [searchType]: searchValue - 0
        })
            //页数
            .limit(msg.pageSize - 0).skip((msg.currentPage - 1) * msg.pageSize);

    }

    const chapages = Math.ceil(chaxunye / msg.pageSize);
    //精确查找
    // const data = await tenantsModel.find(cha);
    //模糊查找

    return {
        tableData, pages, total, chaxunye, chapages
    }
}

module.exports.getState = async function (msg) {
    const data = await tenantsModel.updateOne({ _id: msg._id }, { state: msg.state });
    return data
}